package com.service.auth.impl;

import java.util.List;

import javax.annotation.Resource;

import com.dao.auth.ResDao;
import com.dao.auth.RoleResDao;
import com.dao.auth.UserRoleDao;
import com.model.auth.SResources;
import com.service.auth.IResService;

public class ResServiceImpl implements IResService {
	@Resource
	private ResDao resDao;
	
	
	@Resource
	private RoleResDao roleResDao;
	
	
	public List<SResources> getLeftMenus() throws Exception {
		List<SResources> oneList  = resDao.getResByParentId(0, 1);
		if(oneList!=null && !oneList.isEmpty()){
			for(int i=0;i<oneList.size() ;i++){
				SResources res = oneList.get(i);
				List<SResources> childs = resDao.getResByParentId(res.getId(), 1);
				res.setChilds(childs);
			}
		}
		return oneList;
	}
	
	
	
	/**
	 * 
	 * @param id
	 * @param status
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<SResources> getResByParentId(Integer id,Integer status) throws Exception{
		
		return resDao.getResByParentId(id, status);
	}
	
	/**
	 * 查询所有的栏目树
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<SResources> getAllMenus() throws Exception{
		
		
		return resDao.getAllMenus();
	}
	
	
	@SuppressWarnings("unchecked")
	public List<SResources> getResPageInfo(int parentId,int pageNO,int pageSize) throws Exception{
		
		return resDao.getResPageInfo(parentId, pageNO, pageSize);
	}
	

	public int getResConunt(int parentId)throws Exception{
		
		return resDao.getResConunt(parentId);
	}
	
	
	public SResources getResById(int id) throws Exception{
		
		return resDao.getResById(id);
	}
	
	/**
	 * 根据ID 删除角色 关联删除角色资源表 用户角色表
	 */
	public boolean delResById(int id) throws Exception{
		
		
		List<SResources> resList = resDao.getResByParentId(id, null);
		if(resList!=null && !resList.isEmpty()){
			for(int i=0;i<resList.size();i++){
				SResources res = resList.get(i);
				delResById(res.getId());
			}
		}
		return resDao.delResById(id) && roleResDao.delAllResByResId(id) ;
	}
	
	
	
	
	
	
	//递归删除
	public boolean insertRes(SResources resources) throws Exception{
		
		return resDao.insertRes(resources);
	}
	
	
	public boolean updateRes(SResources resources) throws Exception{
		
		return resDao.updateRes(resources);
	}
}
